package cn.sstech.IM.server.group;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.ChannelGroupFuture;
import io.netty.channel.group.ChannelMatcher;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;
public class ServerGroupManager {

	static Logger logger = LoggerFactory.getLogger(ServerGroupManager.class);
	
	/**
	 * 服务中所有在线群，key为群的数据库id
	 */
	private static final Map<Long,ChannelGroup> SERVER_GROUPS = new HashMap<Long,ChannelGroup>();
	
	/**
	 * 根据群的数据库id查找群对象
	 * @param groupId
	 * @return
	 */
	public static ChannelGroup getGroup(Long groupId ){
		ChannelGroup channelGroup = SERVER_GROUPS.get(groupId);
		if(null == channelGroup){
			channelGroup = new DefaultChannelGroup(String.valueOf(groupId), GlobalEventExecutor.INSTANCE);
			SERVER_GROUPS.put(groupId, channelGroup);
		}
		
		return channelGroup;
	}
	
	/**
	 * 根据群的数据库id删除群对象
	 * @param groupId
	 */
	public static void removeGroup(Long groupId ){
		/**
		 * jvm自己释放组，不释放组内链接
		 */
		SERVER_GROUPS.remove(groupId);
	}
	
    public static void addChannel(Channel channel,Long groupId ) {
    	if(null != SERVER_GROUPS.get(groupId)){
    		SERVER_GROUPS.get(groupId).add(channel);
    	}
    	else{
    		logger.warn(String.valueOf(groupId)+"群组不存在，无法添加channel");
    	}
    	
    }
    
    public static void addChannel(List<Channel> channel,ChannelGroup channelGroup ) {
    	if(null!=channelGroup){
    		channelGroup.addAll(channel);
    	}
    	
    }
    
    /**
     * 弃用无用链接
     * @param channel
     * @param groupId
     */
    public static void removeChannel(Channel channel,Long groupId ) {
    	if(null != SERVER_GROUPS.get(groupId)){
    		SERVER_GROUPS.get(groupId).remove(channel);
    		
    		//如果没有链接则释放对应群
    		if(!SERVER_GROUPS.get(groupId).iterator().hasNext()){
    			removeGroup(groupId);
    		}
    	}
    	else{
    		logger.warn(String.valueOf(groupId)+"群组不存在，无法删除channel");
    	}
    }
    
    /**
     * 是否包含channel
     * @param channel
     * @param groupId
     */
    public static boolean existChannel(Channel channel,Long groupId ) {
    	if(null != SERVER_GROUPS.get(groupId)){
    		boolean result  = SERVER_GROUPS.get(groupId).contains(channel);
    		return result;
    	}
    	else{
    		logger.warn(String.valueOf(groupId)+"群组不存在，无法确定channel");
    		return false;
    	}
    }
    
    /**
     * 广播 
     * @param msg
     * @return
     */
    public static ChannelGroupFuture broadcast(Object msg,Long groupId) {
    	ChannelGroup channelGroup = SERVER_GROUPS.get(groupId);
    	if(null != channelGroup){
    		return channelGroup.writeAndFlush(msg);
    	}
    	else{
    		logger.warn(String.valueOf(groupId)+"群组不存在，无法广播");
    		return null;
    	}
    }
    /**
     * 广播
     * @param msg
     * @param matcher
     * @return
     */
    public static ChannelGroupFuture broadcast(Object msg, ChannelMatcher matcher,Long groupId) {
    	ChannelGroup channelGroup = SERVER_GROUPS.get(groupId);
    	if(null != channelGroup){
    		return channelGroup.writeAndFlush(msg, matcher);
    	}
    	else{
    		logger.warn(String.valueOf(groupId)+"群组不存在，无法广播");
    		return null;
    	}
    }
    
    /**
     * 获取服务中在线群的数量
     * @return
     */
    public static Integer getGroupCount(){
    	return SERVER_GROUPS.size();
    }
}
